
var base = require('../base');
var test = require('../test');
var event = require('../event');

var a = event.Variable();
var b = event.Variable();
var and = false;
var or = false;
var xor = false;
var not = true;

a.and(b).observe(function (value) { and = !!value; });
a.or(b).observe(function (value) { or = !!value; });
a.xor(b).observe(function (value) { xor = !!value; });
a.not().observe(function (value) { not = !!value; });
a.observe(function (value) { not = !!value; });

test.assertEq(and, false, '0 and');
test.assertEq(or, false, '0 or');
test.assertEq(xor, false, '0 xor');
test.assertEq(not, false, '0 not');

a.set(10);

test.assertEq(and, false, '1 and');
test.assertEq(or, true, '1 or');
test.assertEq(xor, true, '1 xor');
test.assertEq(not, true, '1 not');

b.set(20);

test.assertEq(and, true, '2 and');
test.assertEq(or, true, '2 or');
test.assertEq(xor, false, '2 xor');

var x = event.Promise();
var a = 0;
x.observe(function () {
    a = x.get();
});
test.assertEq(a, 0, 'pre-promise');
x.resolve(10);
test.assertEq(a, 10, 'post-promise');
var sent;
x.observe(function () {
    if (sent)
        throw new Error("post-promise resignaled");
    a = 20;
    sent = true;
});
test.assertEq(a, 20, 'post-promise reobserved');
x.resolve(30);

var y = event.Promise();
var b = 0;
x.and(y).observe(function () {
    b++;
});
test.assertEq(b, 0, 'combined pre-promise');
y.resolve(10);
test.assertEq(b, 1, 'combined post-promise');

var a = event.Variable(10);
var b = event.Variable(20);
var c = a.add(b);
test.assertEq(c.get(), 30, 'variable add');
a.set(20);
test.assertEq(c.get(), 40, 'variable add after reset');

